package servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.GraphSearchCriteria;
import service.impl.Converter;
import service.impl.graph.GraphConstructor;

/**
 * Servlet implementation class GraphData
 */
public class GraphData extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public GraphData() {
		super();
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("application/json");
		response.setHeader("Cache-Control", "no-cache");

		HttpSession session = request.getSession(true);
		//		User user = (User) (session.getAttribute("currentSessionUser"));
		// FIXME add user.id()
		GraphSearchCriteria criteria = collectSearchCriteria(request, "B04963");// user.getCode());

		if (criteria == null) {
			System.out.println("Something wrong with search criteria");
			return;
		}
		try {
			String graphsJSONData = GraphConstructor.collectGraphsData(criteria);
			//			System.out.println(graphsJSONData);
			response.getWriter().write(graphsJSONData);
		} catch (Throwable theException) {
			System.out.println(theException);
		}
	}

	private GraphSearchCriteria collectSearchCriteria(HttpServletRequest request, String currentUserCode) {
		GraphSearchCriteria criteria = new GraphSearchCriteria();
		criteria.setPeriodFrom(Converter.intoDate(request.getParameter("periodFrom")));
		criteria.setPeriodTo(Converter.intoDate(request.getParameter("periodTo")));
		criteria.setCourseCode(request.getParameter("courseCode"));
		String methodId = request.getParameter("methodId");
		if (Converter.isNotEmpty(methodId)) {
			criteria.setMethodId(Long.parseLong(methodId));
		}
		criteria.setStudentCode(request.getParameter("studentCode"));
		criteria.setGroup(request.getParameter("group"));
		if (!isCriteriaValid(criteria, currentUserCode)) {
			return null;
		}
		return criteria;
	}

	private boolean isCriteriaValid(GraphSearchCriteria criteria, String currentUserCode) {
		// Period is compulsory (at least one)
		if (criteria.getPeriodFrom() == null && criteria.getPeriodTo() == null) {
			return false;
		}

		// If group is given, course must be given too
		if (Converter.isNotEmpty(criteria.getGroup()) && Converter.isEmpty(criteria.getCourseCode())) {
			return false;
		}

		// If studentCode and group are not given, then take into account only
		// current user; also if both are given, then nullify group value and
		// set current user code as studentCode
		// this actually is already controlled in JavaScript, but here we
		// control it one more time
		if ((Converter.isEmpty(criteria.getStudentCode()) && Converter.isEmpty(criteria.getGroup()))
				|| (Converter.isNotEmpty(criteria.getStudentCode()) && Converter.isNotEmpty(criteria.getGroup()))) {
			criteria.setStudentCode(currentUserCode);
			criteria.setGroup(null);
		}
		return true;
	}

}
